package com.wonders.module.auth.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wonders.common.constant.TipConstant;
import com.wonders.framework.entity.vo.Page;
import com.wonders.framework.util.StringUtil;
import com.wonders.framework.util.web.RequestUtil;
import com.wonders.framework.web.BaseController;
import com.wonders.module.auth.entity.vo.UserRoleVo;
import com.wonders.module.auth.service.UserRoleService;

/**
 * 用户角色控制层
 * 
 * @author WuBin
 * 
 */
@Controller
@RequestMapping("/auth/userRole")
public class UserRoleController extends BaseController {

	@Autowired
	private UserRoleService userRoleService;

	/**
	 * 导航到角色用户授权页面
	 * 
	 * @return
	 */
	@RequestMapping("toRoleUser")
	public Object toRoleUser() {
		return "auth/roleUser";
	}

	/**
	 * 查询角色关联用户信息
	 * 
	 * @return
	 */
	@RequestMapping("queryRoleUser")
	@ResponseBody
	public Object queryRoleUser() {
		UserRoleVo userRoleVo = RequestUtil
				.requestParamToBean(UserRoleVo.class);
		Page page = userRoleService.queryRoleUser(userRoleVo,
				super.getDataGridPage());
		return super.dataGridResult(page);
	}

	/**
	 * 保存角色关联用户信息
	 * 
	 * @return
	 */
	@RequestMapping("addRoleUser")
	@ResponseBody
	public Object addRoleUser() {
		String keys = (String) RequestUtil.getRequestParam("keys");
		Long roleId = Long.valueOf((String) RequestUtil
				.getRequestParam("roleId"));
		if (StringUtils.isEmpty(keys) || roleId == null) {
			return super.ajaxError(TipConstant.PARAM.MISSING);
		}
		userRoleService.addRoleUser(roleId,
				StringUtil.stringToList(keys, Long.class));
		return super.ajaxSuccess();
	}

	/**
	 * 删除角色关联用户信息
	 * 
	 * @return
	 */
	@RequestMapping("removeRoleUser")
	@ResponseBody
	public Object removeRoleUser() {
		String keys = (String) RequestUtil.getRequestParam("keys");
		Long roleId = Long.valueOf((String) RequestUtil
				.getRequestParam("roleId"));
		if (StringUtils.isEmpty(keys) || roleId == null) {
			return super.ajaxError(TipConstant.PARAM.MISSING);
		}
		userRoleService.removeRoleUser(roleId,
				StringUtil.stringToList(keys, Long.class));
		return super.ajaxSuccess();
	}

	/**
	 * 导航到用户角色授权页面
	 * 
	 * @return
	 */
	@RequestMapping("toUserRole")
	public Object toUserRole() {
		return "auth/userRole";
	}

	/**
	 * 查询用户关联角色信息
	 * 
	 * @return
	 */
	@RequestMapping("queryUserRole")
	@ResponseBody
	public Object queryUserRole() {
		UserRoleVo userRoleVo = RequestUtil
				.requestParamToBean(UserRoleVo.class);
		Page page = userRoleService.queryUserRole(userRoleVo,
				super.getDataGridPage());
		return super.dataGridResult(page);
	}

	/**
	 * 保存用户关联角色信息
	 * 
	 * @return
	 */
	@RequestMapping("addUserRole")
	@ResponseBody
	public Object addUserRole() {
		String keys = (String) RequestUtil.getRequestParam("keys");
		Long userId = Long.valueOf((String) RequestUtil
				.getRequestParam("userId"));
		if (StringUtils.isEmpty(keys) || userId == null) {
			return super.ajaxError(TipConstant.PARAM.MISSING);
		}
		userRoleService.addUserRole(userId,
				StringUtil.stringToList(keys, Long.class));
		return super.ajaxSuccess();
	}

	/**
	 * 删除用户关联角色信息
	 * 
	 * @return
	 */
	@RequestMapping("removeUserRole")
	@ResponseBody
	public Object removeUserRole() {
		String keys = (String) RequestUtil.getRequestParam("keys");
		Long userId = Long.valueOf((String) RequestUtil
				.getRequestParam("userId"));
		if (StringUtils.isEmpty(keys) || userId == null) {
			return super.ajaxError(TipConstant.PARAM.MISSING);
		}
		userRoleService.removeUserRole(userId,
				StringUtil.stringToList(keys, Long.class));
		return super.ajaxSuccess();
	}

}
